package com.yl.zqy.controller.sys;

import com.yl.zqy.common.CustomException;
import com.yl.zqy.common.Result;
import com.yl.zqy.constant.ZQYConstant;
import com.yl.zqy.entity.sys.SysDictData;
import com.yl.zqy.enums.ResultCode;
import com.yl.zqy.service.sys.SysDictDataService;
import com.yl.zqy.vo.common.IPageVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * <p>
 * 字典数据 前端控制器
 * </p>
 *
 * @author liang.yu
 * @since 2024-03-25
 */
@RestController
@RequestMapping("/sysDictData")
@Tag(name = "字典数据管理", description = "SysDictDataController")
@RequiredArgsConstructor
public class SysDictDataController {
    private final SysDictDataService dictDataService;

    @PostMapping("/add")
    @Operation(summary = "新增字典数据", tags = "字典数据管理")
    public Result<Boolean> insertDictData(@RequestBody SysDictData dictData) {
        Optional.ofNullable(dictData.getDictCode()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "字典类型不能为空！"));
        Optional.ofNullable(dictData.getDictTypeId()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "字典类型ID不能为空！"));
        return dictDataService.insertDictData(dictData);
    }

    @PutMapping("/update")
    @Operation(summary = "修改字典数据", tags = "字典数据管理")
    public Result<Boolean> updateDictData(@RequestBody SysDictData dictData) {
        Optional.ofNullable(dictData.getId()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "ID不能为空！"));
        Optional.ofNullable(dictData.getDictTypeId()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "字典类型ID不能为空！"));
        Optional.ofNullable(dictData.getDictCode()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "字典类型不能为空！"));
        Optional.ofNullable(dictData.getDictLabel()).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "字典标签不能为空！"));
        return dictDataService.updateDictData(dictData);
    }

    @DeleteMapping("/deleteById/{id}")
    @Operation(summary = "删除字典数据", tags = "字典数据管理")
    public Result<Boolean> deleteById(@Parameter(description = "id") @PathVariable(value = "id") String id) {
        Optional.ofNullable(id).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "ID不能为空！"));

        boolean result = dictDataService.removeById(id);
        if (result)
            return Result.ok();
        return Result.error("操作失败！");
    }

    @DeleteMapping("/deleteBatchByIds")
    @Operation(summary = "批量删除字典数据", tags = "字典数据管理")
    public Result<Boolean> deleteByIds(
            @Parameter(description = "ids集合") @RequestBody List<String> ids) {
        Optional.ofNullable(ids).orElseThrow(() -> new CustomException(ResultCode.PARAMS_ERROR, "ID不能为空！"));
        boolean result = dictDataService.removeBatchByIds(ids);
        if (result)
            return Result.ok();
        return Result.error("操作失败！");
    }

    @GetMapping("/dictDataByPage")
    @Operation(summary = "获取字典数据分页数据", tags = "字典数据管理")
    public Result<IPageVo<SysDictData>> dictDataByPage(
            @Parameter(description = "页号") @RequestParam(value = "current", defaultValue = ZQYConstant.CURRENT) int current,
            @Parameter(description = "页大小") @RequestParam(value = "size", defaultValue = ZQYConstant.SIZE) int size,
            @Parameter(description = "字典类型ID") @RequestParam(value = "dictTypeId", required = false) String dictTypeId,
            @Parameter(description = "字典类型") @RequestParam(value = "dictCode", required = false) String dictCode,
            @Parameter(description = "条件查询（字典标签or字典值）") @RequestParam(value = "query", required = false) String query) {

        return dictDataService.dictDataByPage(current, size, dictTypeId, dictCode, query);
    }

    @PostMapping("/getDictInfo")
    @Operation(summary = "获取字典数据缓存数据", tags = "字典数据管理")
    public Result<Map<String, List<SysDictData>>> getDictInfo(@Parameter(description = "字典类型编码Code集合") @RequestBody(required = false) List<String> codes) {

        return dictDataService.getDictInfo(codes);
    }

    @GetMapping("/refreshDictCache")
    @Operation(summary = "刷新字典数据缓存数据", tags = "字典数据管理")
    public Result<Boolean> refreshDictCache() {
        return dictDataService.refreshDictDataCache();
    }
}
